/**
 * 
 */
package com.idea.zd.sys.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.idea.zd.sys.constant.Const;
import com.idea.zd.sys.service.PermissionService;
import com.idea.zd.sys.util.CommonUtils;
import com.idea.zd.sys.util.DateUtil;
import com.idea.zd.sys.util.RetDTO;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author y.z
 */
public class BaseController extends Controller {
	private static final Logger log = Logger.getLogger(BaseController.class);
	protected int curPage = 1;
	protected RetDTO ret;
	
	/**
	 * 数据获取成功
	 */
	protected void msgSuccess(){
		ret = new RetDTO();
		ret.setCode(Const.SUCCESS_CODE);
		ret.setMsg(Const.SUCCESS_MSG);
		renderJson(ret);
	}
	
	/**
	 * 数据获取成功
	 * @param code
	 * @param msg
	 * @param mapData
	 */
	protected void msgSuccess(String code,String msg,Map<Object, Object> mapData){
		ret = new RetDTO();
		ret.setCode(code);
		ret.setMsg(msg);
		ret.setData(mapData);
		renderJson(ret);
	}
	
	/**
	 * 数据获取成功
	 * @param mapData
	 */
	protected void msgSuccess(Map<String, Object> mapData){
		ret = new RetDTO();
		ret.setCode(Const.SUCCESS_CODE);
		ret.setMsg(Const.SUCCESS_MSG);
		ret.setData(mapData);
		renderJson(ret);
	}

	/**
	 * 数据获取成功
	 * @param
	 */
	protected void msgSuccess(Object object){
		Map<String, Object> ret = new HashMap<>();
		ret.put("code",Const.SUCCESS_CODE);
		ret.put("msg",Const.SUCCESS_MSG);
		ret.put("data",object);
		renderJson(ret);
	}


	
	/**
	 * 添加成功
	 */
	protected void msgAdd(){
		ret = new RetDTO();
		ret.setCode(Const.SUCCESS_CODE);
		ret.setMsg(Const.ADD_SUCCESS_MSG);
		renderJson(ret);
	}
	
	/**
	 * 更新成功
	 */
	protected void msgUpdate(){
		ret = new RetDTO();
		ret.setCode(Const.SUCCESS_CODE);
		ret.setMsg(Const.UPDATE_SUCCESS_MSG);
		renderJson(ret);
	}
	
	/**
	 * 必传参数为空
	 */
	protected void msgParamNull(){
		ret = new RetDTO();
		ret.setCode(Const.ISBLANK_ERROR_CODE);
		ret.setMsg(Const.ISBLANK_ERROR_MSG);
		renderJson(ret);
	}
	
	/**
	 * 自定义
	 * @param msg
	 * @return
	 */
	protected void msgSetVal(String msg){
		ret = new RetDTO();
		ret.setCode(Const.OTHER_ERROR_CODE);
		ret.setMsg(msg);
		renderJson(ret);
	}
	
	protected void msgSetVal(String msg,Map<Object,Object> mapData){
		ret = new RetDTO();
		ret.setCode(Const.OTHER_ERROR_CODE);
		ret.setMsg(msg);
		ret.setData(mapData);
		renderJson(ret);
	}
	
	/**
	 * 表格表页
	 * @param page
	 */
	protected void setPage(Page<?> page){
		Map<Object, Object> map = new HashMap<Object,Object>();
		map.put("totalPage",page.getTotalPage());		
		map.put("curPage",page.getPageNumber());		
		map.put("list", page.getList());
		msgSuccess(map);
	}
	
	/**
	 * 获取导航
	 */
	protected void getNavigation(){
		String url = getRequest().getRequestURI();
		
		PermissionService permissionService = new PermissionService();
		
		setAttr("navigation", permissionService.getNavigation(url.substring(1,url.length())));
	}
	
	/**
	 * 获取图片
	 * @return
	 */
	public String uploadImg(String field){
		File file = null;
		
		try {
			file = super.getFile(field).getFile();
		} catch (Exception e) {
			file = null;
		}
		
		// 图片上传
		if (null != file) {
			
			String path = "/upload/images" + "/"+ DateUtil.formatDate(new Date(), "yyyyMMdd") + "/"; 

			Map<String, String> imaPath = CommonUtils.uploadFile(getRequest(),file,path);

			return imaPath.get("serverPath");

		}
		return null;
	}
	
	

}
